package com.armed.client.interfaz;

import init.org.util.Constants;
import init.org.util.gui.inputverifiers.LocaleCurrencyInputVerifier;
import init.org.util.logging.LoggerManager;
import java.awt.Component;
import java.text.ParseException;
import java.util.logging.Level;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * Class ColumnPrecioTableRenderer
 *
 *
 * @author init() software
 * @version 22/01/2008 
 */
public class ColumnPrecioTableRenderer extends DefaultTableCellRenderer
{
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (value == null || value.equals(""))
        {
            ((JLabel) component).setText("");
        }
        else
        {
            if (value instanceof Double)
            {
                JLabel lbl = new JLabel();
                Double val = ((Double) value);
                if (val.doubleValue() < 0)
                {
                    lbl.setForeground(Constants.FOREGROUND_ERROR_COLOR);
                }
                else
                {
                    lbl.setForeground(Constants.FOREGROUND_FINE_COLOR);
                }
                lbl.setText(Constants.CURRENCY_HEADING + val);
                return lbl;
            }
            if (value instanceof String)
            {
                JLabel lbl = new JLabel();
                String val = ((String) value).replaceFirst("\\" + Constants.CURRENCY_HEADING, "");
                try
                {
                    Number n = LocaleCurrencyInputVerifier.parseStringToCurrency(val);
                    if (n.doubleValue() < 0)
                    {
                        lbl.setForeground(Constants.FOREGROUND_ERROR_COLOR);
                    }
                    else
                    {
                        lbl.setForeground(Constants.FOREGROUND_FINE_COLOR);
                    }
                    lbl.setText(Constants.CURRENCY_HEADING + val);
                }
                catch (ParseException e)
                {
                    LoggerManager.getLogger().log(Level.SEVERE, "Se ha producido un error al parsear un valor de una celda, el renderer ha sido llamado antes que el editor", e);
                }
                return lbl;
            }
        }
        return component;
    }
}
